<!doctype html>
<html lang="en" data-color-mode="dark">
<head>
<meta charset="utf-8">
<title>Jest 备忘清单
 &#x26;  jest cheatsheet &#x26;  Quick Reference</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta description="Jest 是一款优雅、简洁的 JavaScript 测试框架，这里介绍了它的入门和 一些 API 的索引


入门，为开发人员分享快速参考备忘单。">
<meta keywords="jest,reference,Quick,Reference,cheatsheet,cheat,sheet">
<link rel="icon" href="data:image/svg+xml,%3Csvg%20viewBox%3D%220%200%2024%2024%22%20fill%3D%22none%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20height%3D%221em%22%20width%3D%221em%22%3E%20%3Cpath%20d%3D%22m21.66%2010.44-.98%204.18c-.84%203.61-2.5%205.07-5.62%204.77-.5-.04-1.04-.13-1.62-.27l-1.68-.4c-4.17-.99-5.46-3.05-4.48-7.23l.98-4.19c.2-.85.44-1.59.74-2.2%201.17-2.42%203.16-3.07%206.5-2.28l1.67.39c4.19.98%205.47%203.05%204.49%207.23Z%22%20fill%3D%22%23c9d1d9%22%2F%3E%20%3Cpath%20d%3D%22M15.06%2019.39c-.62.42-1.4.77-2.35%201.08l-1.58.52c-3.97%201.28-6.06.21-7.35-3.76L2.5%2013.28c-1.28-3.97-.22-6.07%203.75-7.35l1.58-.52c.41-.13.8-.24%201.17-.31-.3.61-.54%201.35-.74%202.2l-.98%204.19c-.98%204.18.31%206.24%204.48%207.23l1.68.4c.58.14%201.12.23%201.62.27Zm2.43-8.88c-.06%200-.12-.01-.19-.02l-4.85-1.23a.75.75%200%200%201%20.37-1.45l4.85%201.23a.748.748%200%200%201-.18%201.47Z%22%20fill%3D%22%23228e6c%22%20%2F%3E%20%3Cpath%20d%3D%22M14.56%2013.89c-.06%200-.12-.01-.19-.02l-2.91-.74a.75.75%200%200%201%20.37-1.45l2.91.74c.4.1.64.51.54.91-.08.34-.38.56-.72.56Z%22%20fill%3D%22%23228e6c%22%20%2F%3E%20%3C%2Fsvg%3E" type="image/svg+xml">
<link rel="stylesheet" href="../style/style.css">
<link rel="stylesheet" href="../style/katex.css">
</head>
<body><nav class="header-nav"><div class="max-container"><a href="../index.html" class="logo"><svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" height="1em" width="1em">
  <path d="m21.66 10.44-.98 4.18c-.84 3.61-2.5 5.07-5.62 4.77-.5-.04-1.04-.13-1.62-.27l-1.68-.4c-4.17-.99-5.46-3.05-4.48-7.23l.98-4.19c.2-.85.44-1.59.74-2.2 1.17-2.42 3.16-3.07 6.5-2.28l1.67.39c4.19.98 5.47 3.05 4.49 7.23Z" fill="#c9d1d9"></path>
  <path d="M15.06 19.39c-.62.42-1.4.77-2.35 1.08l-1.58.52c-3.97 1.28-6.06.21-7.35-3.76L2.5 13.28c-1.28-3.97-.22-6.07 3.75-7.35l1.58-.52c.41-.13.8-.24 1.17-.31-.3.61-.54 1.35-.74 2.2l-.98 4.19c-.98 4.18.31 6.24 4.48 7.23l1.68.4c.58.14 1.12.23 1.62.27Zm2.43-8.88c-.06 0-.12-.01-.19-.02l-4.85-1.23a.75.75 0 0 1 .37-1.45l4.85 1.23a.748.748 0 0 1-.18 1.47Z" fill="#228e6c"></path>
  <path d="M14.56 13.89c-.06 0-.12-.01-.19-.02l-2.91-.74a.75.75 0 0 1 .37-1.45l2.91.74c.4.1.64.51.54.91-.08.34-.38.56-.72.56Z" fill="#228e6c"></path>
</svg>
<span class="title">Quick Reference</span></a><div class="menu"><a href="javascript:void(0);" class="searchbtn" id="searchbtn"><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 18 18">
  <path fill="currentColor" d="M17.71,16.29 L14.31,12.9 C15.4069846,11.5024547 16.0022094,9.77665502 16,8 C16,3.581722 12.418278,0 8,0 C3.581722,0 0,3.581722 0,8 C0,12.418278 3.581722,16 8,16 C9.77665502,16.0022094 11.5024547,15.4069846 12.9,14.31 L16.29,17.71 C16.4777666,17.8993127 16.7333625,18.0057983 17,18.0057983 C17.2666375,18.0057983 17.5222334,17.8993127 17.71,17.71 C17.8993127,17.5222334 18.0057983,17.2666375 18.0057983,17 C18.0057983,16.7333625 17.8993127,16.4777666 17.71,16.29 Z M2,8 C2,4.6862915 4.6862915,2 8,2 C11.3137085,2 14,4.6862915 14,8 C14,11.3137085 11.3137085,14 8,14 C4.6862915,14 2,11.3137085 2,8 Z"></path>
</svg><span>搜索</span><span>⌘K</span></a><a href="https://github.com/jaywcjlove/reference/blob/main/docs/jest.md" class="" target="__blank"><svg viewBox="0 0 36 36" fill="currentColor" height="1em" width="1em"><path d="m33 6.4-3.7-3.7a1.71 1.71 0 0 0-2.36 0L23.65 6H6a2 2 0 0 0-2 2v22a2 2 0 0 0 2 2h22a2 2 0 0 0 2-2V11.76l3-3a1.67 1.67 0 0 0 0-2.36ZM18.83 20.13l-4.19.93 1-4.15 9.55-9.57 3.23 3.23ZM29.5 9.43 26.27 6.2l1.85-1.85 3.23 3.23Z"></path><path fill="none" d="M0 0h36v36H0z"></path></svg><span>编辑</span></a><button id="darkMode" type="button"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="light" height="1em" width="1em">
  <path d="M6.995 12c0 2.761 2.246 5.007 5.007 5.007s5.007-2.246 5.007-5.007-2.246-5.007-5.007-5.007S6.995 9.239 6.995 12zM11 19h2v3h-2zm0-17h2v3h-2zm-9 9h3v2H2zm17 0h3v2h-3zM5.637 19.778l-1.414-1.414 2.121-2.121 1.414 1.414zM16.242 6.344l2.122-2.122 1.414 1.414-2.122 2.122zM6.344 7.759 4.223 5.637l1.415-1.414 2.12 2.122zm13.434 10.605-1.414 1.414-2.122-2.122 1.414-1.414z"></path>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 24 24" class="dark" height="1em" width="1em">
  <path d="M12 11.807A9.002 9.002 0 0 1 10.049 2a9.942 9.942 0 0 0-5.12 2.735c-3.905 3.905-3.905 10.237 0 14.142 3.906 3.906 10.237 3.905 14.143 0a9.946 9.946 0 0 0 2.735-5.119A9.003 9.003 0 0 1 12 11.807z"></path>
</svg>
</button><script src="../js/dark.js?v=1.5.2"></script><a href="https://github.com/jaywcjlove/reference" class="" target="__blank"><svg viewBox="0 0 16 16" fill="currentColor" height="1em" width="1em"><path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.012 8.012 0 0 0 16 8c0-4.42-3.58-8-8-8z"></path></svg></a></div></div></nav><div class="wrap h1body-exist max-container"><header class="wrap-header h1wrap"><h1 id="jest-备忘清单"><svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" fill="currentColor" height="1em" width="1em">
  <path d="M22.251 11.82a3.117 3.117 0 0 0-2.328-3.01L22.911 0H8.104L11.1 8.838a3.116 3.116 0 0 0-2.244 2.988 3.12 3.12 0 0 0 1.313 2.536 8.279 8.279 0 0 1-1.084 1.244 8.14 8.14 0 0 1-2.55 1.647c-.834-.563-1.195-1.556-.869-2.446a3.11 3.11 0 0 0-.91-6.08 3.117 3.117 0 0 0-3.113 3.113c0 .848.347 1.626.903 2.182-.048.097-.097.195-.146.299-.465.959-.993 2.043-1.195 3.259-.403 2.432.257 4.384 1.849 5.489A5.093 5.093 0 0 0 5.999 24c1.827 0 3.682-.917 5.475-1.807 1.279-.632 2.599-1.292 3.898-1.612.48-.118.98-.187 1.508-.264 1.07-.153 2.175-.312 3.168-.89a4.482 4.482 0 0 0 2.182-3.091c.174-.994 0-1.994-.444-2.87.298-.48.465-1.042.465-1.647zm-1.355 0c0 .965-.785 1.75-1.75 1.75a1.753 1.753 0 0 1-1.085-3.126l.007-.007c.056-.042.118-.084.18-.125 0 0 .008 0 .008-.007.028-.014.055-.035.083-.05.007 0 .014-.006.021-.006.028-.014.063-.028.097-.042.035-.014.07-.027.098-.041.007 0 .013-.007.02-.007.028-.007.056-.021.084-.028.007 0 .02-.007.028-.007.034-.007.062-.014.097-.02h.007l.104-.022c.007 0 .02 0 .028-.007.028 0 .055-.007.083-.007h.035c.035 0 .07-.007.111-.007h.09c.028 0 .05 0 .077.007h.014c.055.007.111.014.167.028a1.766 1.766 0 0 1 1.396 1.723zM10.043 1.39h10.93l-2.509 7.4c-.104.02-.208.055-.312.09l-2.64-5.385-2.648 5.35c-.104-.034-.216-.055-.327-.076l-2.494-7.38zm4.968 9.825a3.083 3.083 0 0 0-.938-1.668l1.438-2.904 1.452 2.967c-.43.43-.743.98-.868 1.605H15.01zm-3.481-1.098c.034-.007.062-.014.097-.02h.02c.029-.008.056-.008.084-.015h.028c.028 0 .049-.007.076-.007h.271c.028 0 .049.007.07.007.014 0 .02 0 .035.007.027.007.048.007.076.014.007 0 .014 0 .028.007l.097.02h.007c.028.008.056.015.083.029.007 0 .014.007.028.007.021.007.049.014.07.027.007 0 .014.007.02.007.028.014.056.021.084.035h.007a.374.374 0 0 1 .09.049h.007c.028.014.056.034.084.048.007 0 .007.007.013.007.028.014.05.035.077.049l.007.007c.083.062.16.132.236.201l.007.007a1.747 1.747 0 0 1 .48 1.209 1.752 1.752 0 0 1-3.502 0 1.742 1.742 0 0 1 1.32-1.695zm-6.838-.049c.966 0 1.751.786 1.751 1.751s-.785 1.751-1.75 1.751-1.752-.785-1.752-1.75.786-1.752 1.751-1.752zm16.163 6.025a3.07 3.07 0 0 1-1.508 2.133c-.758.438-1.689.577-2.669.716a17.29 17.29 0 0 0-1.64.291c-1.445.355-2.834 1.05-4.182 1.717-1.724.854-3.35 1.66-4.857 1.66a3.645 3.645 0 0 1-2.154-.688c-1.529-1.056-1.453-3.036-1.272-4.12.167-1.015.632-1.966 1.077-2.877.028-.055.049-.104.077-.16.152.056.312.098.479.126-.264 1.473.486 2.994 1.946 3.745l.264.139.284-.104c1.216-.431 2.342-1.133 3.336-2.071a9.334 9.334 0 0 0 1.445-1.716c.16.027.32.034.48.034a3.117 3.117 0 0 0 3.008-2.327h1.167a3.109 3.109 0 0 0 3.01 2.327c.576 0 1.11-.16 1.57-.43.18.52.236 1.063.139 1.605z"></path>
</svg>
<a aria-hidden="true" tabindex="-1" href="#jest-备忘清单"><span class="icon icon-link"></span></a>Jest 备忘清单</h1><div class="wrap-body">
<p><a href="https://www.npmjs.com/package/jest"><img src="https://img.shields.io/npm/v/jest.svg?style=flat" alt="NPM version"></a>
<a href="https://www.npmjs.com/package/jest"><img src="https://img.shields.io/npm/dm/jest.svg?style=flat" alt="Downloads"></a>
<a href="https://github.com/facebook/jest/network/dependents"><img src="https://badgen.net/github/dependents-repo/facebook/jest" alt="Repo Dependents"></a>
<a href="https://github.com/facebook/jest"><img src="https://badgen.net/badge/icon/Github?icon=github&#x26;label" alt="Github repo"></a></p>
<p style="padding-top: 12px;">Jest 是一款优雅、简洁的 JavaScript 测试框架，这里介绍了它的入门和 一些 API 的索引</p>
<!--rehype:style=padding-top: 12px;-->
</div></header><div class="menu-tocs"><div class="menu-btn"><svg aria-hidden="true" fill="currentColor" height="1em" width="1em" viewBox="0 0 16 16" version="1.1" data-view-component="true">
  <path fill-rule="evenodd" d="M2 4a1 1 0 100-2 1 1 0 000 2zm3.75-1.5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zM3 8a1 1 0 11-2 0 1 1 0 012 0zm-1 6a1 1 0 100-2 1 1 0 000 2z"></path>
</svg></div><div class="menu-modal"><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#入门">入门</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#介绍">介绍</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#快速开始">快速开始</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#常用命令">常用命令</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#支持驼峰和串式命名的参数">支持驼峰和串式命名的参数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#编写测试">编写测试</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#bdd-语法">BDD 语法</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#测试结构">测试结构</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#聚焦测试">聚焦测试</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#可选参数">可选参数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#跳过测试">跳过测试</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#基本测试用例">基本测试用例</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#快照">快照</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#errors">Errors</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#objects">Objects</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#objects-1">Objects</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#numbers">Numbers</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#booleans">Booleans</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#strings">Strings</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#nan">NaN</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#others">Others</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#匹配器">匹配器</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#基本匹配器">基本匹配器</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#真实性">真实性</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#数字">数字</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#字符串">字符串</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#数组">数组</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#对象">对象</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#模拟函数">模拟函数</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#别名">别名</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#杂项">杂项</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#快照-1">快照</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#promise-匹配器jest-20">Promise 匹配器（Jest 20+）</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#例外">例外</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#别名-1">别名</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#异步测试">异步测试</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#实例">实例</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#asyncawait">async/await</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#done-回调">done() 回调</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#promises">Promises</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#模拟">模拟</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#模拟函数-1">模拟函数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#返回解析和拒绝值">返回、解析和拒绝值</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#使用-jestmock-方法模拟模块">使用 jest.mock 方法模拟模块</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#使用模拟文件模拟模块">使用模拟文件模拟模块</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#模拟-getters-和-setters">模拟 getters 和 setters</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#模拟-getter-和-setter-jest-2210">模拟 getter 和 setter (Jest 22.1.0+)</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#定时器模拟">定时器模拟</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#模拟对象方法">模拟对象方法</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#清除和恢复模拟">清除和恢复模拟</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#使用模拟时访问原始模块">使用模拟时访问原始模块</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#数据驱动测试jest-23">数据驱动测试（Jest 23+）</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#使用不同的数据运行相同的测试">使用不同的数据运行相同的测试</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#使用模板文字相同">使用模板文字相同</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#或在describe级别">或在“describe”级别</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#跳过测试-1">跳过测试</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#不要运行这些测试">不要运行这些测试</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#仅运行以下测试">仅运行以下测试</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#测试有副作用的模块">测试有副作用的模块</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#实例-1">实例</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#命令参数参考">命令参数参考</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#命令参数">命令参数</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#另见">另见</a></div></div><div class="h1wrap-body"><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="入门"><a aria-hidden="true" tabindex="-1" href="#入门"><span class="icon icon-link"></span></a>入门</h2><div class="wrap-body">
<!--rehype:body-class=cols-6-->
</div></div><div class="h2wrap-body cols-6"><div class="wrap h3body-not-exist col-span-2 row-span-2"><div class="wrap-header h3wrap"><h3 id="介绍"><a aria-hidden="true" tabindex="-1" href="#介绍"><span class="icon icon-link"></span></a>介绍</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2 row-span-2-->
<p><a href="https://jestjs.io/">Jest v29</a> 是一款优雅、简洁的 JavaScript 测试框架。</p>
<ul>
<li><code>无需配置</code> 大多数 JS 项目中即装即用，无需配置</li>
<li><code>优秀接口</code> 从 <code>it</code> 到 <code>expect</code> - Jest 将工具包整合在一处。文档齐全、不断维护，非常不错。</li>
<li><code>隔离的</code> 并行进行测试，发挥每一丝算力。</li>
<li><code>快照</code> 轻松编写持续追踪大型对象的测试，并在测试旁或代码内显示实时快照。</li>
<li><code>代码覆盖</code> 无需其他操作，您仅需添加 <code>--coverage</code> 参数来生成代码覆盖率报告。</li>
</ul>
</div></div></div><div class="wrap h3body-not-exist col-span-2 row-span-2"><div class="wrap-header h3wrap"><h3 id="快速开始"><a aria-hidden="true" tabindex="-1" href="#快速开始"><span class="icon icon-link"></span></a>快速开始</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2 row-span-2-->
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token function">npm</span> <span class="token function">install</span> --save-dev jest
</span></code></pre>
<p>Add to <code>package.json</code></p>
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token string-property property">"scripts"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token string-property property">"test"</span><span class="token operator">:</span> <span class="token string">"jest"</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span></code></pre>
<p>运行你的测试</p>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token function">npm</span> <span class="token builtin class-name">test</span> -- <span class="token parameter variable">--watch</span>
</span></code></pre>
<p>查看: <a href="https://jestjs.io/docs/getting-started">Getting started</a></p>
</div></div></div><div class="wrap h3body-not-exist col-span-2 row-span-2"><div class="wrap-header h3wrap"><h3 id="常用命令"><a aria-hidden="true" tabindex="-1" href="#常用命令"><span class="icon icon-link"></span></a>常用命令</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2 row-span-2-->
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token comment"># 指定测试文件的名称</span>
</span><span class="code-line">$ jest my-test
</span><span class="code-line"><span class="token comment"># 指定测试文件的路径</span>
</span><span class="code-line">$ jest path/to/my-test.js
</span><span class="code-line"><span class="token comment"># 仅运行在 hg/git 上有改动但尚未提交的文件</span>
</span><span class="code-line">$ jest <span class="token parameter variable">-o</span>
</span><span class="code-line"><span class="token comment"># 仅运行与 fileA.js 和 fileB.js相关的测试</span>
</span><span class="code-line">$ jest <span class="token parameter variable">--findRelatedTests</span> fileA.js fileB.js
</span><span class="code-line"><span class="token comment"># 仅运行匹配特定名称的测试用例</span>
</span><span class="code-line">$ jest <span class="token parameter variable">-t</span> name-of-spec
</span><span class="code-line"><span class="token comment"># 运行监视模式默认执行 jest -o 监视有改动的测试</span>
</span><span class="code-line">$ jest <span class="token parameter variable">--watch</span> 
</span><span class="code-line">$ jest <span class="token parameter variable">--watchAll</span> <span class="token comment"># 监视所有测试</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="支持驼峰和串式命名的参数"><a aria-hidden="true" tabindex="-1" href="#支持驼峰和串式命名的参数"><span class="icon icon-link"></span></a>支持驼峰和串式命名的参数</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token comment"># 下面给出的命令执行的结果是一样的：</span>
</span><span class="code-line">$ jest --collect-coverage
</span><span class="code-line">$ jest <span class="token parameter variable">--collectCoverage</span>
</span><span class="code-line"><span class="token comment"># 不同命名形式的参数还可以混用，如：</span>
</span><span class="code-line">$ jest --update-snapshot <span class="token punctuation">\</span>
</span><span class="code-line">       <span class="token parameter variable">--detectOpenHandles</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="编写测试"><a aria-hidden="true" tabindex="-1" href="#编写测试"><span class="icon icon-link"></span></a>编写测试</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token function">describe</span><span class="token punctuation">(</span><span class="token string">'My work'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'works'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token function">expect</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toEqual</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="bdd-语法"><a aria-hidden="true" tabindex="-1" href="#bdd-语法"><span class="icon icon-link"></span></a>BDD 语法</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token function">describe</span><span class="token punctuation">(</span><span class="token string">'My work'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>
</span><span class="code-line highlight-line">  <span class="token function">it</span><span class="token punctuation">(</span><span class="token string">'works'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span> <span class="token comment">// `it`是`test`的别名</span>
</span><span class="code-line">    ···
</span><span class="code-line">  <span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-3 row-span-2"><div class="wrap-header h3wrap"><h3 id="测试结构"><a aria-hidden="true" tabindex="-1" href="#测试结构"><span class="icon icon-link"></span></a>测试结构</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-3 row-span-2-->
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token function">describe</span><span class="token punctuation">(</span><span class="token string">'makePoniesPink'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token function">beforeAll</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token comment">/* 在所有测试之前运行 */</span>
</span><span class="code-line">  <span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token function">afterAll</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token comment">/* 在所有测试后运行 */</span>
</span><span class="code-line">  <span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token function">beforeEach</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token comment">/* 在每次测试之前运行 */</span>
</span><span class="code-line">  <span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token function">afterEach</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token comment">/* 每次测试后运行 */</span>
</span><span class="code-line">  <span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'make each pony pink'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token keyword">const</span> actual <span class="token operator">=</span> <span class="token function">fn</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'Alice'</span><span class="token punctuation">,</span> <span class="token string">'Bob'</span><span class="token punctuation">,</span> <span class="token string">'Eve'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</span><span class="code-line">    <span class="token function">expect</span><span class="token punctuation">(</span>actual<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toEqual</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'Pink Alice'</span><span class="token punctuation">,</span> <span class="token string">'Pink Bob'</span><span class="token punctuation">,</span> <span class="token string">'Pink Eve'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="聚焦测试"><a aria-hidden="true" tabindex="-1" href="#聚焦测试"><span class="icon icon-link"></span></a>聚焦测试</h3><div class="wrap-body">
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line">describe<span class="token punctuation">.</span><span class="token method function property-access">only</span><span class="token punctuation">(</span>···<span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line">it<span class="token punctuation">.</span><span class="token method function property-access">only</span><span class="token punctuation">(</span>···<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 别名: fit()</span>
</span></code></pre>
<p>查看: <a href="https://jestjs.io/docs/api#testonlyname-fn-timeout">test.only</a></p>
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="可选参数"><a aria-hidden="true" tabindex="-1" href="#可选参数"><span class="icon icon-link"></span></a>可选参数</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->

























<table><thead><tr><th>Flag</th><th>Description</th></tr></thead><tbody><tr><td><code>--coverage</code></td><td>查看测试覆盖率摘要</td></tr><tr><td><code>--detectOpenHandles</code></td><td>查看未关闭端口的摘要</td></tr><tr><td><code>--runInBand</code></td><td>一个接一个地运行所有测试</td></tr><tr><td><code>--bail,-b</code></td><td>失败跳出测试</td></tr></tbody></table>
<p>更多参数查看 <a href="https://jestjs.io/docs/next/cli#--bailn">Jest CLI Options</a></p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="跳过测试"><a aria-hidden="true" tabindex="-1" href="#跳过测试"><span class="icon icon-link"></span></a>跳过测试</h3><div class="wrap-body">
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line">describe<span class="token punctuation">.</span><span class="token method function property-access">skip</span><span class="token punctuation">(</span>···<span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line">it<span class="token punctuation">.</span><span class="token method function property-access">skip</span><span class="token punctuation">(</span>···<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 别名: xit()</span>
</span></code></pre>
<p>查看: <a href="https://jestjs.io/docs/next/api#testskipname-fn">test.skip</a></p>
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="基本测试用例"><a aria-hidden="true" tabindex="-1" href="#基本测试用例"><span class="icon icon-link"></span></a>基本测试用例</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token property-access">not</span><span class="token punctuation">.</span><span class="token method function property-access">toBe</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">.</span><span class="token method function property-access">toEqual</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">.</span><span class="token method function property-access">toBeTruthy</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// Errors 测试</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toThrow</span><span class="token punctuation">(</span>error<span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">.</span><span class="token method function property-access">toThrowErrorMatchingSnapshot</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="快照"><a aria-hidden="true" tabindex="-1" href="#快照"><span class="icon icon-link"></span></a>快照</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">.</span><span class="token method function property-access">toMatchSnapshot</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">.</span><span class="token method function property-access">toMatchInlineSnapshot</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="errors"><a aria-hidden="true" tabindex="-1" href="#errors"><span class="icon icon-link"></span></a>Errors</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">.</span><span class="token method function property-access">toThrow</span><span class="token punctuation">(</span>error<span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">.</span><span class="token method function property-access">toThrowErrorMatchingSnapshot</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="objects"><a aria-hidden="true" tabindex="-1" href="#objects"><span class="icon icon-link"></span></a>Objects</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">.</span><span class="token method function property-access">toBeInstanceOf</span><span class="token punctuation">(</span><span class="token maybe-class-name">Class</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">.</span><span class="token method function property-access">toMatchObject</span><span class="token punctuation">(</span>object<span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">.</span><span class="token method function property-access">toHaveProperty</span><span class="token punctuation">(</span>keyPath<span class="token punctuation">,</span> value<span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="objects-1"><a aria-hidden="true" tabindex="-1" href="#objects-1"><span class="icon icon-link"></span></a>Objects</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">.</span><span class="token method function property-access">toContain</span><span class="token punctuation">(</span>item<span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">.</span><span class="token method function property-access">toContainEqual</span><span class="token punctuation">(</span>item<span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">.</span><span class="token method function property-access">toHaveLength</span><span class="token punctuation">(</span>number<span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="numbers"><a aria-hidden="true" tabindex="-1" href="#numbers"><span class="icon icon-link"></span></a>Numbers</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">.</span><span class="token method function property-access">toBeCloseTo</span><span class="token punctuation">(</span>number<span class="token punctuation">,</span> numDigits<span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">.</span><span class="token method function property-access">toBeGreaterThan</span><span class="token punctuation">(</span>number<span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">.</span><span class="token method function property-access">toBeGreaterThanOrEqual</span><span class="token punctuation">(</span>number<span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">.</span><span class="token method function property-access">toBeLessThan</span><span class="token punctuation">(</span>number<span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">.</span><span class="token method function property-access">toBeLessThanOrEqual</span><span class="token punctuation">(</span>number<span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="booleans"><a aria-hidden="true" tabindex="-1" href="#booleans"><span class="icon icon-link"></span></a>Booleans</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">.</span><span class="token method function property-access">toBeFalsy</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">.</span><span class="token method function property-access">toBeNull</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">.</span><span class="token method function property-access">toBeTruthy</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">.</span><span class="token method function property-access">toBeUndefined</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">.</span><span class="token method function property-access">toBeDefined</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="strings"><a aria-hidden="true" tabindex="-1" href="#strings"><span class="icon icon-link"></span></a>Strings</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">.</span><span class="token method function property-access">toMatch</span><span class="token punctuation">(</span>regexpOrString<span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="nan"><a aria-hidden="true" tabindex="-1" href="#nan"><span class="icon icon-link"></span></a>NaN</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'当值为 NaN 时通过'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token function">expect</span><span class="token punctuation">(</span><span class="token number">NaN</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toBeNaN</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">  <span class="token function">expect</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token property-access">not</span><span class="token punctuation">.</span><span class="token method function property-access">toBeNaN</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist col-span-2"><div class="wrap-header h3wrap"><h3 id="others"><a aria-hidden="true" tabindex="-1" href="#others"><span class="icon icon-link"></span></a>Others</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2-->
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line">expect<span class="token punctuation">.</span><span class="token method function property-access">extend</span><span class="token punctuation">(</span>matchers<span class="token punctuation">)</span>
</span><span class="code-line">expect<span class="token punctuation">.</span><span class="token method function property-access">any</span><span class="token punctuation">(</span>constructor<span class="token punctuation">)</span>
</span><span class="code-line">expect<span class="token punctuation">.</span><span class="token method function property-access">addSnapshotSerializer</span><span class="token punctuation">(</span>serializer<span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line">expect<span class="token punctuation">.</span><span class="token method function property-access">assertions</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="匹配器"><a aria-hidden="true" tabindex="-1" href="#匹配器"><span class="icon icon-link"></span></a>匹配器</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="基本匹配器"><a aria-hidden="true" tabindex="-1" href="#基本匹配器"><span class="icon icon-link"></span></a>基本匹配器</h3><div class="wrap-body">
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span><span class="token number">42</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toBe</span><span class="token punctuation">(</span><span class="token number">42</span><span class="token punctuation">)</span>    <span class="token comment">// 严格相等 (===)</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span><span class="token number">42</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token property-access">not</span><span class="token punctuation">.</span><span class="token method function property-access">toBe</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span> <span class="token comment">// 严格相等 (!==)</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toEqual</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment">// 深度相等</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// 深度相等</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token literal-property property">a</span><span class="token operator">:</span> <span class="token keyword nil">undefined</span><span class="token punctuation">,</span> <span class="token literal-property property">b</span><span class="token operator">:</span> <span class="token number">2</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">.</span><span class="token method function property-access">toEqual</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token literal-property property">b</span><span class="token operator">:</span> <span class="token number">2</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// 严格相等 (Jest 23+)</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token literal-property property">a</span><span class="token operator">:</span> <span class="token keyword nil">undefined</span><span class="token punctuation">,</span> <span class="token literal-property property">b</span><span class="token operator">:</span> <span class="token number">2</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">.</span><span class="token property-access">not</span><span class="token punctuation">.</span><span class="token method function property-access">toStrictEqual</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token literal-property property">b</span><span class="token operator">:</span> <span class="token number">2</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
</span></code></pre>
<p><a href="http://jestjs.io/docs/en/using-matchers">Using matchers</a>, <a href="https://jestjs.io/docs/en/expect">matchers docs</a></p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="真实性"><a aria-hidden="true" tabindex="-1" href="#真实性"><span class="icon icon-link"></span></a>真实性</h3><div class="wrap-body">
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token comment">// 匹配 if 语句视为 true 的任何内容</span>
</span><span class="code-line"><span class="token comment">// （not false、0、''、null、undefined、NaN）</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span><span class="token string">'foo'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toBeTruthy</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 匹配 if 语句视为 false 的任何内容</span>
</span><span class="code-line"><span class="token comment">// （false、0、''、null、undefined、NaN）</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toBeFalsy</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 仅匹配 null</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span><span class="token keyword null nil">null</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toBeNull</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 仅匹配未定义</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span><span class="token keyword nil">undefined</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toBeUndefined</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// toBeUndefined 的反义词</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span><span class="token number">7</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toBeDefined</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 匹配真假</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toEqual</span><span class="token punctuation">(</span>expect<span class="token punctuation">.</span><span class="token method function property-access">any</span><span class="token punctuation">(</span><span class="token known-class-name class-name">Boolean</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="数字"><a aria-hidden="true" tabindex="-1" href="#数字"><span class="icon icon-link"></span></a>数字</h3><div class="wrap-body">
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token comment">// 大于</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toBeGreaterThan</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 大于或等于</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toBeGreaterThanOrEqual</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 小于</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toBeLessThan</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 小于或等于</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toBeLessThanOrEqual</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 接近于</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span><span class="token number">0.2</span> <span class="token operator">+</span> <span class="token number">0.1</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toBeCloseTo</span><span class="token punctuation">(</span><span class="token number">0.3</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 原始值的传递类型</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span><span class="token number">NaN</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toEqual</span><span class="token punctuation">(</span>expect<span class="token punctuation">.</span><span class="token method function property-access">any</span><span class="token punctuation">(</span><span class="token known-class-name class-name">Number</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="字符串"><a aria-hidden="true" tabindex="-1" href="#字符串"><span class="icon icon-link"></span></a>字符串</h3><div class="wrap-body">
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token comment">// 检查字符串是否与正则表达式匹配。</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span><span class="token string">'long string'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toMatch</span><span class="token punctuation">(</span><span class="token string">'str'</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span><span class="token string">'string'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toEqual</span><span class="token punctuation">(</span>expect<span class="token punctuation">.</span><span class="token method function property-access">any</span><span class="token punctuation">(</span><span class="token known-class-name class-name">String</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span><span class="token string">'coffee'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toMatch</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">ff</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span><span class="token string">'pizza'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token property-access">not</span><span class="token punctuation">.</span><span class="token method function property-access">toMatch</span><span class="token punctuation">(</span><span class="token string">'coffee'</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'pizza'</span><span class="token punctuation">,</span> <span class="token string">'coffee'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toEqual</span><span class="token punctuation">(</span>
</span><span class="code-line">  <span class="token punctuation">[</span>
</span><span class="code-line">    expect<span class="token punctuation">.</span><span class="token method function property-access">stringContaining</span><span class="token punctuation">(</span><span class="token string">'zz'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> 
</span><span class="code-line">    expect<span class="token punctuation">.</span><span class="token method function property-access">stringMatching</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">ff</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">]</span>
</span><span class="code-line"><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="数组"><a aria-hidden="true" tabindex="-1" href="#数组"><span class="icon icon-link"></span></a>数组</h3><div class="wrap-body">
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toEqual</span><span class="token punctuation">(</span>expect<span class="token punctuation">.</span><span class="token method function property-access">any</span><span class="token punctuation">(</span><span class="token known-class-name class-name">Array</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">const</span> exampleArray <span class="token operator">=</span> <span class="token punctuation">[</span>
</span><span class="code-line">  <span class="token string">'Alice'</span><span class="token punctuation">,</span> <span class="token string">'Bob'</span><span class="token punctuation">,</span> <span class="token string">'Eve'</span>
</span><span class="code-line"><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span>exampleArray<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toHaveLength</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span>exampleArray<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toContain</span><span class="token punctuation">(</span><span class="token string">'Alice'</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span>exampleArray<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toEqual</span><span class="token punctuation">(</span>
</span><span class="code-line">  expect<span class="token punctuation">.</span><span class="token method function property-access">arrayContaining</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string">'Alice'</span><span class="token punctuation">,</span> <span class="token string">'Bob'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">{</span> <span class="token literal-property property">a</span><span class="token operator">:</span> <span class="token number">1</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> <span class="token literal-property property">a</span><span class="token operator">:</span> <span class="token number">2</span> <span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</span><span class="code-line">    <span class="token punctuation">.</span><span class="token method function property-access">toContainEqual</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token literal-property property">a</span><span class="token operator">:</span> <span class="token number">1</span> <span class="token punctuation">}</span><span class="token punctuation">)</span> <span class="token comment">// 包含相等</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="对象"><a aria-hidden="true" tabindex="-1" href="#对象"><span class="icon icon-link"></span></a>对象</h3><div class="wrap-body">
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token literal-property property">a</span><span class="token operator">:</span><span class="token number">1</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toHaveProperty</span><span class="token punctuation">(</span><span class="token string">'a'</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token literal-property property">a</span><span class="token operator">:</span><span class="token number">1</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toHaveProperty</span><span class="token punctuation">(</span><span class="token string">'a'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token literal-property property">a</span><span class="token operator">:</span> <span class="token punctuation">{</span><span class="token literal-property property">b</span><span class="token operator">:</span><span class="token number">1</span><span class="token punctuation">}</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toHaveProperty</span><span class="token punctuation">(</span><span class="token string">'a.b'</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token literal-property property">a</span><span class="token operator">:</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token literal-property property">b</span><span class="token operator">:</span><span class="token number">2</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toMatchObject</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token literal-property property">a</span><span class="token operator">:</span><span class="token number">1</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span><span class="token punctuation">{</span> <span class="token literal-property property">a</span><span class="token operator">:</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token literal-property property">b</span><span class="token operator">:</span><span class="token number">2</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toMatchObject</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token literal-property property">a</span><span class="token operator">:</span> expect<span class="token punctuation">.</span><span class="token method function property-access">any</span><span class="token punctuation">(</span><span class="token known-class-name class-name">Number</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
</span><span class="code-line">  <span class="token literal-property property">b</span><span class="token operator">:</span> expect<span class="token punctuation">.</span><span class="token method function property-access">any</span><span class="token punctuation">(</span><span class="token known-class-name class-name">Number</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
</span><span class="code-line"><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">{</span> <span class="token literal-property property">a</span><span class="token operator">:</span> <span class="token number">1</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> <span class="token literal-property property">b</span><span class="token operator">:</span> <span class="token number">2</span> <span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toEqual</span><span class="token punctuation">(</span><span class="token punctuation">[</span>
</span><span class="code-line">  expect<span class="token punctuation">.</span><span class="token method function property-access">objectContaining</span><span class="token punctuation">(</span>
</span><span class="code-line">    <span class="token punctuation">{</span> <span class="token literal-property property">a</span><span class="token operator">:</span> expect<span class="token punctuation">.</span><span class="token method function property-access">any</span><span class="token punctuation">(</span><span class="token known-class-name class-name">Number</span><span class="token punctuation">)</span> <span class="token punctuation">}</span>
</span><span class="code-line">  <span class="token punctuation">)</span><span class="token punctuation">,</span>
</span><span class="code-line">  expect<span class="token punctuation">.</span><span class="token method function property-access">anything</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
</span><span class="code-line"><span class="token punctuation">]</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="模拟函数"><a aria-hidden="true" tabindex="-1" href="#模拟函数"><span class="icon icon-link"></span></a>模拟函数</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token comment">// const fn = jest.fn()</span>
</span><span class="code-line"><span class="token comment">// const fn = jest.fn().mockName('Unicorn') -- 命名为 mock, Jest 22+</span>
</span><span class="code-line"><span class="token comment">// 函数被调用</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span>fn<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toBeCalled</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 函数*未*调用</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span>fn<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token property-access">not</span><span class="token punctuation">.</span><span class="token method function property-access">toBeCalled</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 函数只被调用一次</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span>fn<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toHaveBeenCalledTimes</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 任何执行都带有这些参数</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span>fn<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toBeCalledWith</span><span class="token punctuation">(</span>arg1<span class="token punctuation">,</span> arg2<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 最后一个执行是用这些参数</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span>fn<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toHaveBeenLastCalledWith</span><span class="token punctuation">(</span>arg1<span class="token punctuation">,</span> arg2<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 第 N 个执行带有这些参数（Jest 23+）</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span>fn<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toHaveBeenNthCalledWith</span><span class="token punctuation">(</span>callNumber<span class="token punctuation">,</span> args<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 函数返回没有抛出错误（Jest 23+）</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span>fn<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toHaveReturnedTimes</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 函数返回一个值（Jest 23+）</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span>fn<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toHaveReturnedWith</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 最后一个函数调用返回一个值（Jest 23+）</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span>fn<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toHaveLastReturnedWith</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 第 N 个函数调用返回一个值（Jest 23+）</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span>fn<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toHaveNthReturnedWith</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span>fn<span class="token punctuation">.</span><span class="token property-access">mock</span><span class="token punctuation">.</span><span class="token property-access">calls</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toEqual</span><span class="token punctuation">(</span><span class="token punctuation">[</span>
</span><span class="code-line">  <span class="token punctuation">[</span><span class="token string">'first'</span><span class="token punctuation">,</span> <span class="token string">'call'</span><span class="token punctuation">,</span> <span class="token string">'args'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
</span><span class="code-line">  <span class="token punctuation">[</span><span class="token string">'second'</span><span class="token punctuation">,</span> <span class="token string">'call'</span><span class="token punctuation">,</span> <span class="token string">'args'</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
</span><span class="code-line"><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment">// 多次调用</span>
</span><span class="code-line"><span class="token comment">// fn.mock.calls[0][0] — 第一次调用的第一个参数</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span>fn<span class="token punctuation">.</span><span class="token property-access">mock</span><span class="token punctuation">.</span><span class="token property-access">calls</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toBe</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span>
</span></code></pre>
<h4 id="别名"><a aria-hidden="true" tabindex="-1" href="#别名"><span class="icon icon-link"></span></a>别名</h4>
<ul>
<li><code>toBeCalled</code> → <code>toHaveBeenCalled</code></li>
<li><code>toBeCalledWith</code> → <code>toHaveBeenCalledWith</code></li>
<li><code>lastCalledWith</code> → <code>toHaveBeenLastCalledWith</code></li>
<li><code>nthCalledWith</code> → <code>toHaveBeenNthCalledWith</code></li>
<li><code>toReturnTimes</code> → <code>toHaveReturnedTimes</code></li>
<li><code>toReturnWith</code> → <code>toHaveReturnedWith</code></li>
<li><code>lastReturnedWith</code> → <code>toHaveLastReturnedWith</code></li>
<li><code>nthReturnedWith</code> → <code>toHaveNthReturnedWith</code></li>
</ul>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="杂项"><a aria-hidden="true" tabindex="-1" href="#杂项"><span class="icon icon-link"></span></a>杂项</h3><div class="wrap-body">
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token comment">// 检查对象是否是类的实例。</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">A</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toBeInstanceOf</span><span class="token punctuation">(</span><span class="token constant">A</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// 检查对象是否是函数的实例。</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toEqual</span><span class="token punctuation">(</span>
</span><span class="code-line">  expect<span class="token punctuation">.</span><span class="token method function property-access">any</span><span class="token punctuation">(</span><span class="token known-class-name class-name">Function</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// 匹配除 null 或 undefined 之外的任何内容</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span><span class="token string">'pizza'</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toEqual</span><span class="token punctuation">(</span>expect<span class="token punctuation">.</span><span class="token method function property-access">anything</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="快照-1"><a aria-hidden="true" tabindex="-1" href="#快照-1"><span class="icon icon-link"></span></a>快照</h3><div class="wrap-body">
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token comment">// 这可确保某个值与最近的快照匹配。</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span>node<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toMatchSnapshot</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// Jest 23+</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span>user<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toMatchSnapshot</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token literal-property property">date</span><span class="token operator">:</span> expect<span class="token punctuation">.</span><span class="token method function property-access">any</span><span class="token punctuation">(</span><span class="token known-class-name class-name">Date</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
</span><span class="code-line"><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// 确保值与最近的快照匹配。</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span>user<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toMatchInlineSnapshot</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="promise-匹配器jest-20"><a aria-hidden="true" tabindex="-1" href="#promise-匹配器jest-20"><span class="icon icon-link"></span></a>Promise 匹配器（Jest 20+）</h3><div class="wrap-body">
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'resolve to lemon'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token comment">// 验证在测试期间是否调用了一定数量的断言。</span>
</span><span class="code-line">  expect<span class="token punctuation">.</span><span class="token method function property-access">assertions</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token comment">// 确保添加return语句</span>
</span><span class="code-line">  <span class="token keyword control-flow">return</span> <span class="token function">expect</span><span class="token punctuation">(</span><span class="token known-class-name class-name">Promise</span><span class="token punctuation">.</span><span class="token method function property-access">resolve</span><span class="token punctuation">(</span><span class="token string">'lemon'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</span><span class="code-line">            <span class="token punctuation">.</span><span class="token property-access">resolves</span><span class="token punctuation">.</span><span class="token method function property-access">toBe</span><span class="token punctuation">(</span><span class="token string">'lemon'</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line">  <span class="token keyword control-flow">return</span> <span class="token function">expect</span><span class="token punctuation">(</span><span class="token known-class-name class-name">Promise</span><span class="token punctuation">.</span><span class="token method function property-access">reject</span><span class="token punctuation">(</span><span class="token string">'octopus'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</span><span class="code-line">            <span class="token punctuation">.</span><span class="token property-access">rejects</span><span class="token punctuation">.</span><span class="token method function property-access">toBeDefined</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line">  <span class="token keyword control-flow">return</span> <span class="token function">expect</span><span class="token punctuation">(</span><span class="token known-class-name class-name">Promise</span><span class="token punctuation">.</span><span class="token method function property-access">reject</span><span class="token punctuation">(</span>
</span><span class="code-line">    <span class="token known-class-name class-name">Error</span><span class="token punctuation">(</span><span class="token string">'pizza'</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token property-access">rejects</span><span class="token punctuation">.</span><span class="token method function property-access">toThrow</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span></code></pre>
<p>或者使用 async/await:</p>
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'resolve to lemon'</span><span class="token punctuation">,</span> <span class="token keyword">async</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>
</span><span class="code-line">  expect<span class="token punctuation">.</span><span class="token method function property-access">assertions</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token keyword control-flow">await</span> <span class="token function">expect</span><span class="token punctuation">(</span><span class="token known-class-name class-name">Promise</span><span class="token punctuation">.</span><span class="token method function property-access">resolve</span><span class="token punctuation">(</span><span class="token string">'lemon'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</span><span class="code-line">          <span class="token punctuation">.</span><span class="token property-access">resolves</span><span class="token punctuation">.</span><span class="token method function property-access">toBe</span><span class="token punctuation">(</span><span class="token string">'lemon'</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line">  <span class="token keyword control-flow">await</span> <span class="token function">expect</span><span class="token punctuation">(</span><span class="token known-class-name class-name">Promise</span><span class="token punctuation">.</span><span class="token method function property-access">resolve</span><span class="token punctuation">(</span><span class="token string">'lemon'</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</span><span class="code-line">          <span class="token punctuation">.</span><span class="token property-access">resolves</span><span class="token punctuation">.</span><span class="token property-access">not</span><span class="token punctuation">.</span><span class="token method function property-access">toBe</span><span class="token punctuation">(</span><span class="token string">'octopus'</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span></code></pre>
<p><a href="https://jestjs.io/docs/en/expect#resolves">resolves 文档</a></p>
</div></div></div><div class="wrap h3body-exist"><div class="wrap-header h3wrap"><h3 id="例外"><a aria-hidden="true" tabindex="-1" href="#例外"><span class="icon icon-link"></span></a>例外</h3><div class="wrap-body">
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token comment">// const fn = () => {</span>
</span><span class="code-line"><span class="token comment">//    throw new Error('Out of cheese!')</span>
</span><span class="code-line"><span class="token comment">// }</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span>fn<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toThrow</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span>fn<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toThrow</span><span class="token punctuation">(</span><span class="token string">'Out of cheese'</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// 测试错误消息在某处说“cheese”：这些是等价的</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span>fn<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toThrowError</span><span class="token punctuation">(</span><span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">cheese</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span>fn<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toThrowError</span><span class="token punctuation">(</span><span class="token string">'cheese'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// 测试准确的错误信息</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span>fn<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toThrowError</span><span class="token punctuation">(</span>
</span><span class="code-line">  <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex"><span class="token anchor function">^</span>Out of cheese!<span class="token anchor function">$</span></span><span class="token regex-delimiter">/</span></span>
</span><span class="code-line"><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span>fn<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toThrowError</span><span class="token punctuation">(</span>
</span><span class="code-line">  <span class="token keyword">new</span> <span class="token class-name">Error</span><span class="token punctuation">(</span><span class="token string">'Out of cheese!'</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">)</span><span class="token punctuation">;</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// 测试函数在调用时是否抛出与最新快照匹配的错误。</span>
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span>fn<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toThrowErrorMatchingSnapshot</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span></code></pre>
<h4 id="别名-1"><a aria-hidden="true" tabindex="-1" href="#别名-1"><span class="icon icon-link"></span></a>别名</h4>
<ul>
<li><code>toThrowError</code> → <code>toThrow</code></li>
</ul>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="异步测试"><a aria-hidden="true" tabindex="-1" href="#异步测试"><span class="icon icon-link"></span></a>异步测试</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="实例"><a aria-hidden="true" tabindex="-1" href="#实例"><span class="icon icon-link"></span></a>实例</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<p>在异步测试中指定一些预期的断言是一个很好的做法，所以如果你的断言根本没有被调用，测试将会失败。</p>
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'async test'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token comment">// 在测试期间恰好调用了三个断言</span>
</span><span class="code-line">  expect<span class="token punctuation">.</span><span class="token method function property-access">assertions</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span> 
</span><span class="code-line">  <span class="token comment">// 或者 - 在测试期间至少调用一个断言</span>
</span><span class="code-line">  expect<span class="token punctuation">.</span><span class="token method function property-access">hasAssertions</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token comment">// 你的异步测试</span>
</span><span class="code-line"><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span></code></pre>
<p>请注意，您也可以在任何 <code>describe</code> 和 <code>test</code> 之外对每个文件执行此操作：</p>
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token function">beforeEach</span><span class="token punctuation">(</span>expect<span class="token punctuation">.</span><span class="token property-access">hasAssertions</span><span class="token punctuation">)</span>
</span></code></pre>
<p>这将验证每个测试用例至少存在一个断言。 它还可以与更具体的 <code>expect.assertions(3)</code> 声明配合使用。
请参阅 Jest 文档中的 <a href="https://jestjs.io/docs/en/tutorial-async">更多示例</a></p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="asyncawait"><a aria-hidden="true" tabindex="-1" href="#asyncawait"><span class="icon icon-link"></span></a>async/await</h3><div class="wrap-body">
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'async test'</span><span class="token punctuation">,</span> <span class="token keyword">async</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>
</span><span class="code-line">  expect<span class="token punctuation">.</span><span class="token method function property-access">assertions</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line">  <span class="token keyword">const</span> result <span class="token operator">=</span> <span class="token keyword control-flow">await</span> <span class="token function">runAsyncOperation</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token function">expect</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toBe</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="done-回调"><a aria-hidden="true" tabindex="-1" href="#done-回调"><span class="icon icon-link"></span></a>done() 回调</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'async test'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">done</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>
</span><span class="code-line">  expect<span class="token punctuation">.</span><span class="token method function property-access">assertions</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line">  <span class="token function">runAsyncOperation</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">  
</span><span class="code-line">  <span class="token function">setTimeout</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token keyword control-flow">try</span> <span class="token punctuation">{</span>
</span><span class="code-line">      <span class="token keyword">const</span> res <span class="token operator">=</span> <span class="token function">getAsyncOperatResult</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">      <span class="token function">expect</span><span class="token punctuation">(</span>res<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toBe</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span>
</span><span class="code-line">      <span class="token function">done</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">    <span class="token punctuation">}</span> <span class="token keyword control-flow">catch</span> <span class="token punctuation">(</span>err<span class="token punctuation">)</span> <span class="token punctuation">{</span>
</span><span class="code-line">      done<span class="token punctuation">.</span><span class="token method function property-access">fail</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
</span><span class="code-line">    <span class="token punctuation">}</span>
</span><span class="code-line">  <span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span></code></pre>
<p>将断言包装在 <code>try/catch</code> 块中，否则 <code>Jest</code> 将忽略失败</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="promises"><a aria-hidden="true" tabindex="-1" href="#promises"><span class="icon icon-link"></span></a>Promises</h3><div class="wrap-body">
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'async test'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>
</span><span class="code-line">  expect<span class="token punctuation">.</span><span class="token method function property-access">assertions</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line">  <span class="token keyword control-flow">return</span> <span class="token function">runAsyncOperation</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">then</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token parameter">result</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token function">expect</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toBe</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span></code></pre>
<p>从你的测试中 <em>返回</em> 一个 <code>Promise</code></p>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="模拟"><a aria-hidden="true" tabindex="-1" href="#模拟"><span class="icon icon-link"></span></a>模拟</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="模拟函数-1"><a aria-hidden="true" tabindex="-1" href="#模拟函数-1"><span class="icon icon-link"></span></a>模拟函数</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="wrap-text "><code class="language-js code-highlight"><span class="code-line"><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'call the callback'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">const</span> callback <span class="token operator">=</span> jest<span class="token punctuation">.</span><span class="token method function property-access">fn</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token function">fn</span><span class="token punctuation">(</span>callback<span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token function">expect</span><span class="token punctuation">(</span>callback<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toBeCalled</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token function">expect</span><span class="token punctuation">(</span>callback<span class="token punctuation">.</span><span class="token property-access">mock</span><span class="token punctuation">.</span><span class="token property-access">calls</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token property-access">baz</span><span class="token punctuation">)</span>
</span><span class="code-line">    <span class="token punctuation">.</span><span class="token method function property-access">toBe</span><span class="token punctuation">(</span><span class="token string">'pizza'</span><span class="token punctuation">)</span> <span class="token comment">// 第一次调用的第二个参数</span>
</span><span class="code-line">  
</span><span class="code-line">  <span class="token comment">// 匹配第一个和最后一个参数，但忽略第二个参数</span>
</span><span class="code-line">  <span class="token function">expect</span><span class="token punctuation">(</span>callback<span class="token punctuation">)</span>
</span><span class="code-line">    <span class="token punctuation">.</span><span class="token method function property-access">toHaveBeenLastCalledWith</span><span class="token punctuation">(</span>
</span><span class="code-line">      <span class="token string">'meal'</span><span class="token punctuation">,</span>
</span><span class="code-line">      expect<span class="token punctuation">.</span><span class="token method function property-access">anything</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
</span><span class="code-line">      <span class="token string">'margarita'</span>
</span><span class="code-line">    <span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span></code></pre>
<!--rehype:className=wrap-text -->
<p>您还可以使用快照：</p>
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'call the callback'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token comment">// mockName 在 Jest 22+ 中可用</span>
</span><span class="code-line">  <span class="token keyword">const</span> callback <span class="token operator">=</span> jest<span class="token punctuation">.</span><span class="token method function property-access">fn</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">    <span class="token punctuation">.</span><span class="token method function property-access">mockName</span><span class="token punctuation">(</span><span class="token string">'Unicorn'</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line">  <span class="token function">fn</span><span class="token punctuation">(</span>callback<span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token function">expect</span><span class="token punctuation">(</span>callback<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toMatchSnapshot</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token comment">// -></span>
</span><span class="code-line">  <span class="token comment">// [MockFunction Unicorn] {</span>
</span><span class="code-line">  <span class="token comment">//   "calls": Array [</span>
</span><span class="code-line">  <span class="token comment">// ...</span>
</span><span class="code-line"><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span></code></pre>
<p>并将实现传递给 <code>jest.fn</code> 函数：</p>
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token keyword">const</span> callback <span class="token operator">=</span> jest<span class="token punctuation">.</span><span class="token method function property-access">fn</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token boolean">true</span><span class="token punctuation">)</span>
</span></code></pre>
<p><a href="https://jestjs.io/docs/en/mock-function-api">模拟函数文档</a></p>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="返回解析和拒绝值"><a aria-hidden="true" tabindex="-1" href="#返回解析和拒绝值"><span class="icon icon-link"></span></a>返回、解析和拒绝值</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<p>您的模拟可以返回值：</p>
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token keyword">const</span> callback
</span><span class="code-line">    <span class="token operator">=</span> jest<span class="token punctuation">.</span><span class="token method function property-access">fn</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">mockReturnValue</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">const</span> callbackOnce
</span><span class="code-line">    <span class="token operator">=</span> jest<span class="token punctuation">.</span><span class="token method function property-access">fn</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">mockReturnValueOnce</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span>
</span></code></pre>
<p>或解析值：</p>
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token keyword">const</span> promise
</span><span class="code-line">    <span class="token operator">=</span> jest<span class="token punctuation">.</span><span class="token method function property-access">fn</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">mockResolvedValue</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">const</span> promiseOnce
</span><span class="code-line">    <span class="token operator">=</span> jest<span class="token punctuation">.</span><span class="token method function property-access">fn</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">mockResolvedValueOnce</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span>
</span></code></pre>
<p>他们甚至可以拒绝值：</p>
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token keyword">const</span> failedPromise <span class="token operator">=</span>
</span><span class="code-line">  jest<span class="token punctuation">.</span><span class="token method function property-access">fn</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">mockRejectedValue</span><span class="token punctuation">(</span><span class="token string">'Error'</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token keyword">const</span> failedPromiseOnce <span class="token operator">=</span>
</span><span class="code-line">  jest<span class="token punctuation">.</span><span class="token method function property-access">fn</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">mockRejectedValueOnce</span><span class="token punctuation">(</span><span class="token string">'Error'</span><span class="token punctuation">)</span>
</span></code></pre>
<p>你甚至可以结合这些：</p>
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token keyword">const</span> callback <span class="token operator">=</span> jest<span class="token punctuation">.</span><span class="token method function property-access">fn</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">        <span class="token punctuation">.</span><span class="token method function property-access">mockReturnValueOnce</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span>
</span><span class="code-line">        <span class="token punctuation">.</span><span class="token method function property-access">mockReturnValue</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// -></span>
</span><span class="code-line"><span class="token comment">//  call 1: false</span>
</span><span class="code-line"><span class="token comment">//  call 2+: true</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="使用-jestmock-方法模拟模块"><a aria-hidden="true" tabindex="-1" href="#使用-jestmock-方法模拟模块"><span class="icon icon-link"></span></a>使用 <code>jest.mock</code> 方法模拟模块</h3><div class="wrap-body">
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token comment">// 原来的 lodash/memoize 应该存在</span>
</span><span class="code-line">jest<span class="token punctuation">.</span><span class="token method function property-access">mock</span><span class="token punctuation">(</span>
</span><span class="code-line">  <span class="token string">'lodash/memoize'</span><span class="token punctuation">,</span>
</span><span class="code-line">  <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">(</span><span class="token parameter">a</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> a
</span><span class="code-line"><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 不需要原始的 lodash/memoize</span>
</span><span class="code-line">jest<span class="token punctuation">.</span><span class="token method function property-access">mock</span><span class="token punctuation">(</span>
</span><span class="code-line">  <span class="token string">'lodash/memoize'</span><span class="token punctuation">,</span>
</span><span class="code-line">  <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">(</span><span class="token parameter">a</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> a<span class="token punctuation">,</span>
</span><span class="code-line">  <span class="token punctuation">{</span> <span class="token literal-property property">virtual</span><span class="token operator">:</span> <span class="token boolean">true</span> <span class="token punctuation">}</span>
</span><span class="code-line"><span class="token punctuation">)</span>
</span></code></pre>
<p>注意：当使用 <code>babel-jest</code> 时，对 <a href="https://jestjs.io/docs/en/jest-object#jestmockmodulename-factory-options"><code>jest.mock</code></a> 的调用将自动提升到代码块的顶部。 如果您想明确避免这种行为，请使用 <code>jest.doMock</code>。</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="使用模拟文件模拟模块"><a aria-hidden="true" tabindex="-1" href="#使用模拟文件模拟模块"><span class="icon icon-link"></span></a>使用模拟文件模拟模块</h3><div class="wrap-body">
<p>创建一个类似 <code>__mocks__/lodash/memoize.js</code> 的文件：</p>
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line">module<span class="token punctuation">.</span><span class="token method-variable function-variable method function property-access">exports</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token parameter">a</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> a
</span></code></pre>
<p>添加到您的测试中：</p>
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line">jest<span class="token punctuation">.</span><span class="token method function property-access">mock</span><span class="token punctuation">(</span><span class="token string">'lodash/memoize'</span><span class="token punctuation">)</span>
</span></code></pre>
<p>注意：当使用 <code>babel-jest</code> 时，对 <code>jest.mock</code> 的调用将自动提升到代码块的顶部。 如果您想明确避免这种行为，请使用 <code>jest.doMock</code>。<a href="https://jestjs.io/docs/en/manual-mocks">手动模拟文档</a></p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="模拟-getters-和-setters"><a aria-hidden="true" tabindex="-1" href="#模拟-getters-和-setters"><span class="icon icon-link"></span></a>模拟 getters 和 setters</h3><div class="wrap-body">
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token keyword">const</span> getTitle <span class="token operator">=</span> jest<span class="token punctuation">.</span><span class="token method function property-access">fn</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token string">'pizza'</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">const</span> setTitle <span class="token operator">=</span> jest<span class="token punctuation">.</span><span class="token method function property-access">fn</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">const</span> <span class="token dom variable">location</span> <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token known-class-name class-name">Object</span><span class="token punctuation">.</span><span class="token method function property-access">defineProperty</span><span class="token punctuation">(</span><span class="token dom variable">location</span><span class="token punctuation">,</span> <span class="token string">'title'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token literal-property property">get</span><span class="token operator">:</span> getTitle<span class="token punctuation">,</span>
</span><span class="code-line">  <span class="token literal-property property">set</span><span class="token operator">:</span> setTitle<span class="token punctuation">,</span>
</span><span class="code-line"><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="模拟-getter-和-setter-jest-2210"><a aria-hidden="true" tabindex="-1" href="#模拟-getter-和-setter-jest-2210"><span class="icon icon-link"></span></a>模拟 getter 和 setter (Jest 22.1.0+)</h3><div class="wrap-body">
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token keyword">const</span> <span class="token dom variable">location</span> <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
</span><span class="code-line"><span class="token keyword">const</span> getTitle <span class="token operator">=</span> jest
</span><span class="code-line">    <span class="token punctuation">.</span><span class="token method function property-access">spyOn</span><span class="token punctuation">(</span><span class="token dom variable">location</span><span class="token punctuation">,</span> <span class="token string">'title'</span><span class="token punctuation">,</span> <span class="token string">'get'</span><span class="token punctuation">)</span>
</span><span class="code-line">    <span class="token punctuation">.</span><span class="token method function property-access">mockImplementation</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token string">'pizza'</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token keyword">const</span> setTitle <span class="token operator">=</span> jest
</span><span class="code-line">    <span class="token punctuation">.</span><span class="token method function property-access">spyOn</span><span class="token punctuation">(</span><span class="token dom variable">location</span><span class="token punctuation">,</span> <span class="token string">'title'</span><span class="token punctuation">,</span> <span class="token string">'set'</span><span class="token punctuation">)</span>
</span><span class="code-line">    <span class="token punctuation">.</span><span class="token method function property-access">mockImplementation</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-3"><div class="wrap-header h3wrap"><h3 id="定时器模拟"><a aria-hidden="true" tabindex="-1" href="#定时器模拟"><span class="icon icon-link"></span></a>定时器模拟</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-3-->
<p>为使用本机计时器函数（<code>setTimeout</code>、<code>setInterval</code>、<code>clearTimeout</code>、<code>clearInterval</code>）的代码编写同步测试。</p>
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token comment">// 启用假计时器</span>
</span><span class="code-line">jest<span class="token punctuation">.</span><span class="token method function property-access">useFakeTimers</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'kill the time'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">const</span> callback <span class="token operator">=</span> jest<span class="token punctuation">.</span><span class="token method function property-access">fn</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token comment">// 运行使用 setTimeout或setInterval 的代码</span>
</span><span class="code-line">  <span class="token keyword">const</span> actual 
</span><span class="code-line">    <span class="token operator">=</span> <span class="token function">someFunctionThatUseTimers</span><span class="token punctuation">(</span>callback<span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token comment">// 快进直到所有定时器都执行完毕</span>
</span><span class="code-line">  jest<span class="token punctuation">.</span><span class="token method function property-access">runAllTimers</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token comment">// 同步检查结果</span>
</span><span class="code-line">  <span class="token function">expect</span><span class="token punctuation">(</span>callback<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toHaveBeenCalledTimes</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span></code></pre>
<p>或者使用 <a href="https://jestjs.io/docs/en/timer-mocks#advance-timers-by-time">advanceTimersByTime()</a> 按时间调整计时器：</p>
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token comment">// 启用假计时器</span>
</span><span class="code-line">jest<span class="token punctuation">.</span><span class="token method function property-access">useFakeTimers</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'kill the time'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token keyword">const</span> callback <span class="token operator">=</span> jest<span class="token punctuation">.</span><span class="token method function property-access">fn</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token comment">// 运行使用 setTimeout或setInterval 的代码</span>
</span><span class="code-line">  <span class="token keyword">const</span> actual 
</span><span class="code-line">    <span class="token operator">=</span> <span class="token function">someFunctionThatUseTimers</span><span class="token punctuation">(</span>callback<span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token comment">// 快进 250 毫秒</span>
</span><span class="code-line">  jest<span class="token punctuation">.</span><span class="token method function property-access">advanceTimersByTime</span><span class="token punctuation">(</span><span class="token number">250</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token comment">// 同步检查结果</span>
</span><span class="code-line">  <span class="token function">expect</span><span class="token punctuation">(</span>callback<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toHaveBeenCalledTimes</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span></code></pre>
<blockquote>
<p>对于特殊情况，请使用 <a href="https://jestjs.io/docs/en/timer-mocks#run-pending-timers">jest.runOnlyPendingTimers()</a>。</p>
</blockquote>
<p><strong>注意：</strong> 您应该在测试用例中调用 <code>jest.useFakeTimers()</code> 以使用其他假计时器方法。</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="模拟对象方法"><a aria-hidden="true" tabindex="-1" href="#模拟对象方法"><span class="icon icon-link"></span></a>模拟对象方法</h3><div class="wrap-body">
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token keyword">const</span> spy <span class="token operator">=</span> jest<span class="token punctuation">.</span><span class="token method function property-access">spyOn</span><span class="token punctuation">(</span>console<span class="token punctuation">,</span> <span class="token string">'log'</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">.</span><span class="token method function property-access">mockImplementation</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span><span class="token console class-name">console</span><span class="token punctuation">.</span><span class="token property-access">log</span><span class="token punctuation">.</span><span class="token property-access">mock</span><span class="token punctuation">.</span><span class="token property-access">calls</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">.</span><span class="token method function property-access">toEqual</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token string">'dope'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'nope'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
</span><span class="code-line">spy<span class="token punctuation">.</span><span class="token method function property-access">mockRestore</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span></code></pre>
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token keyword">const</span> spy <span class="token operator">=</span> jest<span class="token punctuation">.</span><span class="token method function property-access">spyOn</span><span class="token punctuation">(</span>ajax<span class="token punctuation">,</span> <span class="token string">'request'</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">.</span><span class="token method function property-access">mockImplementation</span><span class="token punctuation">(</span>
</span><span class="code-line">    <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token known-class-name class-name">Promise</span><span class="token punctuation">.</span><span class="token method function property-access">resolve</span><span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token literal-property property">success</span><span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token function">expect</span><span class="token punctuation">(</span>spy<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toHaveBeenCalled</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">spy<span class="token punctuation">.</span><span class="token method function property-access">mockRestore</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="清除和恢复模拟"><a aria-hidden="true" tabindex="-1" href="#清除和恢复模拟"><span class="icon icon-link"></span></a>清除和恢复模拟</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<p>对于一个模拟</p>
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token comment">// 清除模拟使用日期</span>
</span><span class="code-line"><span class="token comment">// （fn.mock.calls、fn.mock.instances）</span>
</span><span class="code-line">fn<span class="token punctuation">.</span><span class="token method function property-access">mockClear</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// 清除并删除任何模拟的返回值或实现</span>
</span><span class="code-line">fn<span class="token punctuation">.</span><span class="token method function property-access">mockReset</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment">// 重置并恢复初始实现</span>
</span><span class="code-line">fn<span class="token punctuation">.</span><span class="token method function property-access">mockRestore</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span></code></pre>
<blockquote>
<p>注意：<code>mockRestore</code> 仅适用于由<code>jest.spyOn</code> 创建的模拟。对于所有模拟：</p>
</blockquote>
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token comment">// 清除所有 mock 的 </span>
</span><span class="code-line"><span class="token comment">// mock.calls、mock.instances、</span>
</span><span class="code-line"><span class="token comment">// mock.contexts 和 mock.results 属性。</span>
</span><span class="code-line">jest<span class="token punctuation">.</span><span class="token method function property-access">clearAllMocks</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 重置所有模拟的状态。</span>
</span><span class="code-line">jest<span class="token punctuation">.</span><span class="token method function property-access">resetAllMocks</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 将所有模拟恢复到其原始值。</span>
</span><span class="code-line">jest<span class="token punctuation">.</span><span class="token method function property-access">restoreAllMocks</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="使用模拟时访问原始模块"><a aria-hidden="true" tabindex="-1" href="#使用模拟时访问原始模块"><span class="icon icon-link"></span></a>使用模拟时访问原始模块</h3><div class="wrap-body">
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line">jest<span class="token punctuation">.</span><span class="token method function property-access">mock</span><span class="token punctuation">(</span><span class="token string">'fs'</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 模拟模块</span>
</span><span class="code-line"><span class="token keyword">const</span> fs <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'fs'</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token comment">// 原始模块</span>
</span><span class="code-line"><span class="token keyword">const</span> fs <span class="token operator">=</span> require<span class="token punctuation">.</span><span class="token method function property-access">requireActual</span><span class="token punctuation">(</span><span class="token string">'fs'</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="数据驱动测试jest-23"><a aria-hidden="true" tabindex="-1" href="#数据驱动测试jest-23"><span class="icon icon-link"></span></a>数据驱动测试（Jest 23+）</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="使用不同的数据运行相同的测试"><a aria-hidden="true" tabindex="-1" href="#使用不同的数据运行相同的测试"><span class="icon icon-link"></span></a>使用不同的数据运行相同的测试</h3><div class="wrap-body">
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line">test<span class="token punctuation">.</span><span class="token method function property-access">each</span><span class="token punctuation">(</span><span class="token punctuation">[</span>
</span><span class="code-line">  <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
</span><span class="code-line">  <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
</span><span class="code-line">  <span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
</span><span class="code-line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token string">'.add(%s, %s)'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">a<span class="token punctuation">,</span> b<span class="token punctuation">,</span> expected</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token function">expect</span><span class="token punctuation">(</span>a <span class="token operator">+</span> b<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toBe</span><span class="token punctuation">(</span>expected<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="使用模板文字相同"><a aria-hidden="true" tabindex="-1" href="#使用模板文字相同"><span class="icon icon-link"></span></a>使用模板文字相同</h3><div class="wrap-body">
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line">test<span class="token punctuation">.</span><span class="token property-access">each</span><span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">
</span></span></span><span class="code-line"><span class="token template-string"><span class="token string">  a    | b    | expected
</span></span></span><span class="code-line"><span class="token template-string"><span class="token string">  </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token number">1</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string"> | </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token number">1</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string"> | </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token number">2</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">
</span></span></span><span class="code-line"><span class="token template-string"><span class="token string">  </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token number">1</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string"> | </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token number">2</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string"> | </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token number">3</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">
</span></span></span><span class="code-line"><span class="token template-string"><span class="token string">  </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token number">2</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string"> | </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token number">1</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string"> | </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token number">3</span><span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">
</span></span></span><span class="code-line"><span class="token template-string"><span class="token string"></span><span class="token template-punctuation string">`</span></span><span class="token punctuation">(</span><span class="token string">'returns $expected when $a is added $b'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter"><span class="token punctuation">{</span> a<span class="token punctuation">,</span> b<span class="token punctuation">,</span> expected <span class="token punctuation">}</span></span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token function">expect</span><span class="token punctuation">(</span>a <span class="token operator">+</span> b<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toBe</span><span class="token punctuation">(</span>expected<span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="或在describe级别"><a aria-hidden="true" tabindex="-1" href="#或在describe级别"><span class="icon icon-link"></span></a>或在“describe”级别</h3><div class="wrap-body">
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line">describe<span class="token punctuation">.</span><span class="token method function property-access">each</span><span class="token punctuation">(</span><span class="token punctuation">[</span>
</span><span class="code-line">  <span class="token punctuation">[</span><span class="token string">'mobile'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'tablet'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string">'desktop'</span><span class="token punctuation">]</span>
</span><span class="code-line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">(</span><span class="token string">'checkout flow on %s'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">viewport</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'displays success page'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>
</span><span class="code-line">    <span class="token comment">//</span>
</span><span class="code-line">  <span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span></code></pre>
<p><a href="https://jestjs.io/docs/en/api#describeeachtablename-fn-timeout">describe.each()</a> 文档、<a href="https://jestjs.io/docs/en/api#testeachtablename-fn-timeout">test.each()</a> 文档</p>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="跳过测试-1"><a aria-hidden="true" tabindex="-1" href="#跳过测试-1"><span class="icon icon-link"></span></a>跳过测试</h2><div class="wrap-body">
<!--rehype:body-class=cols-2-->
</div></div><div class="h2wrap-body cols-2"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="不要运行这些测试"><a aria-hidden="true" tabindex="-1" href="#不要运行这些测试"><span class="icon icon-link"></span></a>不要运行这些测试</h3><div class="wrap-body">
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line">describe<span class="token punctuation">.</span><span class="token method function property-access">skip</span><span class="token punctuation">(</span><span class="token string">'makePoniesPink'</span><span class="token spread operator">...</span>
</span><span class="code-line">tests<span class="token punctuation">.</span><span class="token method function property-access">skip</span><span class="token punctuation">(</span><span class="token string">'make each pony pink'</span><span class="token spread operator">...</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="仅运行以下测试"><a aria-hidden="true" tabindex="-1" href="#仅运行以下测试"><span class="icon icon-link"></span></a>仅运行以下测试</h3><div class="wrap-body">
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line">describe<span class="token punctuation">.</span><span class="token method function property-access">only</span><span class="token punctuation">(</span><span class="token string">'makePoniesPink'</span><span class="token spread operator">...</span>
</span><span class="code-line">tests<span class="token punctuation">.</span><span class="token method function property-access">only</span><span class="token punctuation">(</span><span class="token string">'make each pony pink'</span><span class="token spread operator">...</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="测试有副作用的模块"><a aria-hidden="true" tabindex="-1" href="#测试有副作用的模块"><span class="icon icon-link"></span></a>测试有副作用的模块</h2><div class="wrap-body">
<!--rehype:body-class=cols-1-->
</div></div><div class="h2wrap-body cols-1"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="实例-1"><a aria-hidden="true" tabindex="-1" href="#实例-1"><span class="icon icon-link"></span></a>实例</h3><div class="wrap-body">
<pre class="language-js"><code class="language-js code-highlight"><span class="code-line"><span class="token keyword">const</span> modulePath <span class="token operator">=</span> <span class="token string">'../module-to-test'</span>
</span><span class="code-line"><span class="token function">afterEach</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>
</span><span class="code-line">  jest<span class="token punctuation">.</span><span class="token method function property-access">resetModules</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'第一次测试'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token comment">// 准备第一次测试的条件</span>
</span><span class="code-line">  <span class="token keyword">const</span> result <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span>modulePath<span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token function">expect</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toMatchSnapshot</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token function">test</span><span class="token punctuation">(</span><span class="token string">'第二个文本'</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token comment">// 准备第二次测试的条件</span>
</span><span class="code-line">  <span class="token keyword">const</span> <span class="token function-variable function">fn</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token arrow operator">=></span> <span class="token function">require</span><span class="token punctuation">(</span>modulePath<span class="token punctuation">)</span>
</span><span class="code-line">  <span class="token function">expect</span><span class="token punctuation">(</span>fn<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token method function property-access">toThrow</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</span><span class="code-line"><span class="token punctuation">}</span><span class="token punctuation">)</span>
</span></code></pre>
<p><code>Node.js</code> 和 <code>Jest</code> 会缓存你需要的模块。 要测试具有副作用的模块，您需要在测试之间重置模块注册表</p>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="命令参数参考"><a aria-hidden="true" tabindex="-1" href="#命令参数参考"><span class="icon icon-link"></span></a>命令参数参考</h2><div class="wrap-body">
<!--rehype:body-class=cols-1-->
</div></div><div class="h2wrap-body cols-1"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="命令参数"><a aria-hidden="true" tabindex="-1" href="#命令参数"><span class="icon icon-link"></span></a>命令参数</h3><div class="wrap-body">





































































































































































































































































<table class="left-align"><thead><tr><th align="left">:--</th><th>--</th></tr></thead><tbody><tr><td align="left"><code>--bail[=&#x3C;n>]</code>, <code>-b</code></td><td>在 <code>n</code> 个测试套件失败后立即退出测试套件</td></tr><tr><td align="left"><code>--cache</code></td><td>是否使用缓存</td></tr><tr><td align="left"><code>--changedFilesWithAncestor</code></td><td>运行与当前更改和上次提交中所做更改相关的测试</td></tr><tr><td align="left"><code>--changedSince</code></td><td>运行与自提供的分支或提交哈希以来的更改相关的测试</td></tr><tr><td align="left"><code>--ci</code></td><td>指定该参数时，<code>Jest</code> 会认为正在 <code>CI</code> 环境上运行</td></tr><tr><td align="left"><code>--clearCache</code></td><td>删除 <code>Jest</code> 的缓存目录, 然后不运行测试直接退出</td></tr><tr><td align="left"><code>--clearMocks</code></td><td>在每次测试前自动清除模拟的上下文</td></tr><tr><td align="left"><code>--collectCoverageFrom=&#x3C;glob></code></td><td>相对于 <code>rootDir</code> 的 <code>glob</code> 模式匹配需要从中收集覆盖信息的文件</td></tr><tr><td align="left"><code>--colors</code></td><td>即便 <code>stdout</code> 不是 <code>TTY</code> 模式，也要强制高亮显示测试结果</td></tr><tr><td align="left"><code>--config=&#x3C;path></code></td><td>指定配置文件的路径</td></tr><tr><td align="left"><code>--coverage[=&#x3C;boolean>]</code>, <code>--collectCoverage</code></td><td>将测试覆盖率信息输出为报告</td></tr><tr><td align="left"><code>--coverageProvider=&#x3C;provider></code></td><td>指示应该使用哪个提供程序来检测代码的覆盖率</td></tr><tr><td align="left"><code>--debug</code></td><td>打印关于 <code>Jest</code> 配置的调试信息</td></tr><tr><td align="left"><code>--detectOpenHandles</code></td><td>尝试收集并打印打开的句柄以防止 <code>Jest</code> 干净地退出</td></tr><tr><td align="left"><code>--env=&#x3C;environment></code></td><td>所有测试都使用该测试环境设定</td></tr><tr><td align="left"><code>--errorOnDeprecated</code></td><td>使调用已弃用的 <code>API</code> 抛出有用的错误消息</td></tr><tr><td align="left"><code>--expand</code>, <code>-e</code></td><td>使用该参数来对比完整的差异和错误，而非修复</td></tr><tr><td align="left"><code>--filter=&#x3C;file></code></td><td>导出过滤功能的模块的路径</td></tr><tr><td align="left"><code>--findRelatedTests &#x3C;spaceSeparatedListOfSourceFiles></code></td><td>查找并运行涵盖作为参数传入的以空格分隔的源文件列表的测试</td></tr><tr><td align="left"><code>--forceExit</code></td><td>强制 <code>Jest</code> 在所有测试运行完后退出</td></tr><tr><td align="left"><code>--help</code></td><td>显示帮助信息，类似于本页文档</td></tr><tr><td align="left"><code>--ignoreProjects &#x3C;project1> ... &#x3C;projectN></code></td><td>忽略特定的测试项目</td></tr><tr><td align="left"><code>--init</code></td><td>生成一个基础配置文件</td></tr><tr><td align="left"><code>--injectGlobals</code></td><td>将全局变量（<code>expect</code>,<code>test</code>,<code>describe</code>,<code>beforeEach</code>等）插入到全局环境中</td></tr><tr><td align="left"><code>--json</code></td><td>以 <code>JSON</code> 模式显示测试结果</td></tr><tr><td align="left"><code>--lastCommit</code></td><td>运行受上次提交中的文件更改影响的所有测试</td></tr><tr><td align="left"><code>--listTests</code></td><td>以 <code>JSON</code> 数组的形式列出所有将要运行的测试并退出</td></tr><tr><td align="left"><code>--logHeapUsage</code></td><td>记录每个测试后的记录堆使用情况</td></tr><tr><td align="left"><code>--maxConcurrency=&#x3C;num></code></td><td>防止 <code>Jest</code> 同时执行超过指定数量的测试</td></tr><tr><td align="left"><code>--maxWorkers=&#x3C;num>|&#x3C;string></code></td><td>设定运行测试的最大工作池数目</td></tr><tr><td align="left"><code>--noStackTrace</code></td><td>禁止栈跟踪信息在测试结果输出中</td></tr><tr><td align="left"><code>--notify</code></td><td>激活测试结果通知</td></tr><tr><td align="left"><code>--onlyChanged</code> <code>-o</code></td><td>尝试确定根据当前存储库中哪些已经更改的文件来运行的测试</td></tr><tr><td align="left"><code>--outputFile=&#x3C;filename></code></td><td>通过 <code>——json</code> 参数可以将测试结果写入到指定文件</td></tr><tr><td align="left"><code>--passWithNoTests</code></td><td>允许在没有找到文件的情况下通过测试</td></tr><tr><td align="left"><code>--projects &#x3C;path1> ... &#x3C;pathN></code></td><td>从指定路径中找到的一个或多个项目运行测试；也采用路径 <code>globs</code></td></tr><tr><td align="left"><code>--reporters</code></td><td>使用指定的记者进行测试</td></tr><tr><td align="left"><code>--resetMocks</code></td><td>每次测试前自动重置模拟状态</td></tr><tr><td align="left"><code>--restoreMocks</code></td><td>在每次测试前自动恢复模拟状态和实现</td></tr><tr><td align="left"><code>--roots</code></td><td><code>Jest</code> 应该用来搜索文件的目录路径列表</td></tr><tr><td align="left"><code>--runInBand</code>, <code>-i</code></td><td>仅在当前的进程中连续运行所有测试，而非通过创建的子进程的工作池来运行测试</td></tr><tr><td align="left"><code>--runTestsByPath</code></td><td>仅运行使用其确切路径指定的测试</td></tr><tr><td align="left"><code>--selectProjects &#x3C;project1> ... &#x3C;projectN></code></td><td>运行指定的测试项目</td></tr><tr><td align="left"><code>--setupFilesAfterEnv &#x3C;path1> ... &#x3C;pathN></code></td><td>运行某些代码以在每次测试之前配置或设置测试框架的模块的路径列表</td></tr><tr><td align="left"><code>--shard</code></td><td>测试套件分片以 <code>(?&#x3C;shardIndex>\d+)/(?&#x3C;shardCount>\d+)</code> 的格式执行</td></tr><tr><td align="left"><code>--showConfig</code></td><td>输出 <code>Jest</code> 配置，然后退出</td></tr><tr><td align="left"><code>--silent</code></td><td>阻止所有测试通过控制台输出信息</td></tr><tr><td align="left"><code>--testEnvironmentOptions=&#x3C;json string></code></td><td>带有将传递给 <code>testEnvironment</code> 的选项的 <code>JSON</code> 字符串</td></tr><tr><td align="left"><code>--testLocationInResults</code></td><td>向测试结果添加 <code>location</code> 字段</td></tr><tr><td align="left"><code>--testMatch glob1 ... globN</code></td><td><code>Jest</code> 用于检测测试文件的 <code>glob</code> 模式</td></tr><tr><td align="left"><code>--testNamePattern=&#x3C;regex></code>, <code>-t</code></td><td>仅运行名称与正则表达式匹配的测试</td></tr><tr><td align="left"><code>--testPathIgnorePatterns=&#x3C;regex>|[array]</code></td><td>在执行测试之前针对所有测试路径进行测试的单个或一组正则表达式模式字符串</td></tr><tr><td align="left"><code>--testPathPattern=&#x3C;regex></code></td><td>在运行测试前，匹配的 <code>regexp</code> 模式字符串的测试文件路径</td></tr><tr><td align="left"><code>--testRunner=&#x3C;path></code></td><td>允许你指定自定义测试运行程序</td></tr><tr><td align="left"><code>--testSequencer=&#x3C;path></code></td><td>允许您指定自定义测试定序器</td></tr><tr><td align="left"><code>--testTimeout=&#x3C;number></code></td><td>测试的默认超时时间(以毫秒为单位)。默认值：<code>5000</code></td></tr><tr><td align="left"><code>--updateSnapshot</code>, <code>-u</code></td><td>在运行测试中使用这个参数来重新录制每个失败测试的快照</td></tr><tr><td align="left"><code>--useStderr</code></td><td>转移所有输出到 <code>stderr</code> (标准错误输出)</td></tr><tr><td align="left"><code>--verbose</code></td><td>层次显示测试套件中每个测试的结果</td></tr><tr><td align="left"><code>--version</code>, <code>-v</code></td><td>打印版本并退出</td></tr><tr><td align="left"><code>--watch</code></td><td>监视文件更改，并重新运行与已更改的文件相关的测试</td></tr><tr><td align="left"><code>--watchAll</code></td><td>监视文件的更改并在任何更改时重新运行所有测试</td></tr><tr><td align="left"><code>--watchman</code></td><td>是否使用 <code>watchman</code> 进行文件爬取。 默认为 <code>true</code></td></tr></tbody></table>
<!--rehype:className=left-align-->
</div></div></div></div></div><div class="wrap h2body-not-exist"><div class="wrap-header h2wrap"><h2 id="另见"><a aria-hidden="true" tabindex="-1" href="#另见"><span class="icon icon-link"></span></a>另见</h2><div class="wrap-body">
<ul>
<li><a href="https://github.com/sapegin/jest-cheat-sheet">Jest cheat sheet</a> <em>(github.com)</em></li>
</ul>
</div></div><div class="h2wrap-body"></div></div></div><script src="https://giscus.app/client.js" data-repo="jaywcjlove/reference" data-repo-id="R_kgDOID2-Mw" data-category="Q&#x26;A" data-category-id="DIC_kwDOID2-M84CS5wo" data-mapping="pathname" data-strict="0" data-reactions-enabled="1" data-emit-metadata="0" data-input-position="bottom" data-theme="dark" data-lang="zh-CN" crossorigin="anonymous" async></script><div class="giscus"></div></div><footer class="footer-wrap"><footer class="max-container">© 2022 Kenny Wang.</footer></footer><script src="../data.js?v=1.5.2" defer></script><script src="../js/fuse.min.js?v=1.5.2" defer></script><script src="../js/main.js?v=1.5.2" defer></script><div id="mysearch"><div class="mysearch-box"><div class="mysearch-input"><div><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 18 18">
  <path fill="currentColor" d="M17.71,16.29 L14.31,12.9 C15.4069846,11.5024547 16.0022094,9.77665502 16,8 C16,3.581722 12.418278,0 8,0 C3.581722,0 0,3.581722 0,8 C0,12.418278 3.581722,16 8,16 C9.77665502,16.0022094 11.5024547,15.4069846 12.9,14.31 L16.29,17.71 C16.4777666,17.8993127 16.7333625,18.0057983 17,18.0057983 C17.2666375,18.0057983 17.5222334,17.8993127 17.71,17.71 C17.8993127,17.5222334 18.0057983,17.2666375 18.0057983,17 C18.0057983,16.7333625 17.8993127,16.4777666 17.71,16.29 Z M2,8 C2,4.6862915 4.6862915,2 8,2 C11.3137085,2 14,4.6862915 14,8 C14,11.3137085 11.3137085,14 8,14 C4.6862915,14 2,11.3137085 2,8 Z"></path>
</svg><input id="mysearch-input" type="search" placeholder="搜索" autocomplete="off"><div class="mysearch-clear"></div></div><button id="mysearch-close" type="button">搜索</button></div><div class="mysearch-result"><div id="mysearch-menu"></div><div id="mysearch-content"></div></div></div></div></body>
</html>
